package com.zhang.controller.goods;

import com.zhang.goods.entity.Attribute;
import com.zhang.common.util.result.R;
import com.zhang.common.util.result.RUtils;
import com.zhang.goods.service.IAttributeService;
import com.zhang.goods.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性表 前端控制器
 * </p>
 *
 * @author 创世神
 * @since 2023-09-12
 */
@RestController
@RequestMapping("/attrGroup/attribute")
public class AttributeController {
    @Autowired
    private IAttributeService attributeService;

    @PostMapping("add")
    R addAttr(@RequestBody Attribute attribute) {
        R r = attributeService.addAttr(attribute);
        return r;
    }

    @PutMapping("modify")
    R modifyAttr(@RequestBody Attribute attribute) {
        attributeService.updateById(attribute);
        return RUtils.successMsg("修改成功!");
    }

    @GetMapping("get/byCategoryId/{categoryId}/{attrType}")
    R getAttrByCategoryId(@PathVariable Long categoryId, @PathVariable(required = false) Integer attrType) {
        List<AttrVo> list = attributeService.getAttrByCategoryId(categoryId, attrType);
        return RUtils.successData(list);
    }

    @GetMapping("get/noAsso/byCategoryId/{categoryId}")
    R getAttrByCategoryIdAndNoAsso(@PathVariable Long categoryId) {
        List<Attribute> list = attributeService.getAttrByCategoryIdAdnNoAsso(categoryId);
        return RUtils.successData(list);
    }

    @GetMapping("get/byAttrGroupId/{attrGroupId}")
    public R getAttrByAttrgroup(@PathVariable Long attrGroupId) {
        List<Attribute> list = attributeService.getAttrByAttrgroupId(attrGroupId);
        return RUtils.successData(list);
    }
}
